from django.db import models

# Create your models here.


class User(models.Model):
    openid = models.CharField(verbose_name='OpenId',max_length=100)
    name = models.CharField(verbose_name='用户名',max_length=100)
    pic = models.CharField(verbose_name='用户头像',max_length=200)
    favorite = models.ManyToManyField(to='Card')
    books = models.ManyToManyField(to='Book', through='User_Books')

    def __str__(self):
        return self.name


class Card(models.Model):
    title = models.TextField(verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    level = models.IntegerField(verbose_name='等级')
    type = models.CharField(verbose_name='类型', max_length=100)

    def __str__(self):
        return self.title


class Book(models.Model):
    title = models.CharField(verbose_name='标题', max_length=100)
    msg = models.TextField(verbose_name='描述', )
    type = models.CharField(verbose_name='类型', max_length=100)
    pic = models.CharField(verbose_name='图片', max_length=200)
    cards = models.ManyToManyField(to='Card')

    def __str__(self):
        return self.title


class User_Books(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    res = models.IntegerField(verbose_name='剩余卡片')


class Comment(models.Model):
    card = models.ForeignKey(to='Card', verbose_name='卡片', to_field='id',on_delete=models.CASCADE)
    user = models.ForeignKey(to='User', verbose_name='用户', to_field='id',on_delete=models.CASCADE)
    content = models.TextField(verbose_name='内容')
    likes = models.IntegerField(verbose_name='点赞数')

class Plan(models.Model):
    id = models.AutoField(primary_key=True)
    card = models.ForeignKey(to='Card',to_field='id',on_delete=models.CASCADE)
    book = models.ForeignKey(to='Book',to_field='id',on_delete=models.CASCADE)
    user = models.ForeignKey(to='User',to_field='id',on_delete=models.CASCADE)
    lastUpdate = models.DateField(verbose_name='最近更新')
    lastLearn = models.DateField(verbose_name='最近学习')
    count = models.IntegerField(verbose_name='学习次数')
    point = models.IntegerField(verbose_name='分数')
